package com.yx.modules.api.controller;

import com.yx.common.annotation.Log;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.Dto.TbCategoryDto;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdCategory;
import com.yx.system.service.TdBookService;
import com.yx.system.service.TdCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一级分类控制器
 */
@RestController
@RequestMapping("/api/category")
public class TdCategoryController {

    @Autowired
    private TdCategoryService tdCategoryService;

    @Autowired
    private TdBookService tdBookService;

    /**
     * 按ID查询分类
     *
     * @return
     */
    @PostMapping("findById")
    public R findById(int id) {
        return R.ok().put("tdCategory", tdCategoryService.selectByPrimaryKey(id));
    }

    /**
     * 查询所有
     *
     * @param
     * @return
     */
    @AuthIgnore
    @PostMapping("findAllByCondition")
    public R findAllByCondition() {
        List<TdCategory> tdBookList = tdCategoryService.findAll();
        //初始化数据传输对象集合
        List<TbCategoryDto> tbCategoryDtos = new ArrayList<>();
        for (TdCategory tdCategory:tdBookList){
            //初始化数据传输对象
            TbCategoryDto tbCategoryDto = new TbCategoryDto();
            int flag = tdBookService.queryTotalByConditionId(tdCategory.getId());
            //赋值
            tbCategoryDto.setCount(flag);
            tbCategoryDto.setTdCategory(tdCategory);
            //加入集合
            tbCategoryDtos.add(tbCategoryDto);
        }
        return R.ok().put("data", tbCategoryDtos);
    }
}
